class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :current_user
  

  # exception notifier:
  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception, :with => :render_error
    rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
    rescue_from ActionController::RoutingError, :with => :render_not_found
    # rescue_from ActionController::UnkownController, :with => :render_not_found
    rescue_from ActionController::UnknownAction, :with => :render_not_found
  end

  def routing_error
    render_not_found(nil)
  end


  
  protected
    def render_not_found(exception)
      render :template => "/errors/404.html.erb", :layout => "application.html.erb", :status => 404
    end
    def render_error(exception)
      ExceptionNotifier::Notifier.exception_notification(request.env,exception).deliver
      render :template => "/errors/500.html.erb", :layout => "application.html.erb", :status => 500
    end

  private
    def current_user
      unless cookies[:user_id].blank?
        user = User.find_by_token( cookies[:user_id] )
        if user
          @current_user ||= user
        else
          return false
        end
      else
        false
      end
    end
    def confirm_logged_in
      unless current_user
        redirect_to :controller => 'acceder', :action => 'index'
        return false
      else
        return true
      end
    end


end